package logic

import (
	"context"
	"jinrigaoping/rpc/sys/internal/code"
	"jinrigaoping/rpc/sys/internal/model"

	"jinrigaoping/rpc/sys/internal/svc"
	"jinrigaoping/rpc/sys/sys"

	"github.com/zeromicro/go-zero/core/logx"
)

type SysDictAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSysDictAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SysDictAddLogic {
	return &SysDictAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SysDictAddLogic) SysDictAdd(in *sys.SysDictAddReq) (*sys.SysDictAddRes, error) {

	//判断字典编号是否存在
	sysDict, err := l.svcCtx.SysDictModel.FindOneByDictType(l.ctx, in.DicType)
	if err != nil {
		return nil, err
	}

	if sysDict != nil {
		return nil, code.DictTypeExist
	}

	if _, err := l.svcCtx.SysDictModel.Insert(l.ctx, &model.SysDict{
		DictType: in.DicType,
		DictName: in.DicName,
		Status:   1,
	}); err != nil {
		return nil, err
	}

	return &sys.SysDictAddRes{}, nil
}
